package com.rjkj.medicalwaste.ui.home

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.rjkj.medicalwaste.databinding.DialogOpenBoxBinding
import com.rjkj.medicalwaste.databinding.DialogPrintBinding
import com.rjkj.medicalwaste.databinding.FragmentDeviceDataTestBinding
import com.rjkj.medicalwaste.livedatabus.LiveDataBus
import com.rjkj.medicalwaste.utils.Encoder03Small
import com.rjkj.medicalwaste.viewmodels.BluetoothControlViewModel
import kotlinx.coroutines.delay


class PrintDialog:DialogFragment() {
    private val viewModel:BluetoothControlViewModel by activityViewModels()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val binding = DialogPrintBinding.inflate(inflater, container, false)
        binding.data = viewModel
        binding.lifecycleOwner = viewLifecycleOwner
        onSubscribeUi(binding)
        return binding.root
    }
     fun onSubscribeUi(binding: DialogPrintBinding) {
         LiveDataBus.get().with("Print", Encoder03Small::class.java).observe(viewLifecycleOwner){

         }
         viewModel.decoder04Bean.observe(viewLifecycleOwner,{
             binding.tvState.text = it.weight
         })
         lifecycleScope.launchWhenResumed {
             delay(800L)
//             viewModel.printRec()
         }
         binding.btReprint.setOnClickListener {
             lifecycleScope.launchWhenResumed {
                 delay(400L)
//                 viewModel.printRec()
             }
         }
    }
    override fun onStart() {
        dialog!!.window!!.apply {
//            attributes.width = ViewGroup.LayoutParams.MATCH_PARENT //宽度铺满
            attributes.gravity = Gravity.CENTER//位于底部
            decorView.background = ColorDrawable(Color.TRANSPARENT)//背景色
        }
        super.onStart()
    }

}